<?php

namespace app\admin\controller;

use app\api\controller\Myshopify;
use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * shopify导入订单管理
 *
 * @icon fa fa-circle-o
 */
class FeditionOrdershopifyd extends Backend
{

    /**
     * FeditionOrdershopifyd模型对象
     * @var \app\admin\model\FeditionOrdershopifyd
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new \app\admin\model\FeditionOrdershopifyd;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("timeList", $this->model->gettimeList());

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $where2['tracking_company'] = '4PX';

            $list = $this->model
                ->where($where)
                ->where($where2)
                ->order($sort, $order)
                ->paginate($limit);
            $lists = $list->items();
            if($lists){
                foreach ($lists as $k=>$v){
                    $accounturl = Db::name('fedition_accounturl')->where('id', '=', $v['accounturl'])->find();
                    if($accounturl){
                        $lists[$k]['accounturl_name'] = $accounturl['name'];
                    }else{
                        $lists[$k]['accounturl_name'] = '店铺名称已删除';
                    }
                }
            }

            $Statistics  = Db::name('fedition_ordershopify')
                ->where($where)
                ->where($where2)
                ->field('id,tracking_status,count(*) as tracking_count')
                ->group('tracking_status')
                ->select();

            $newstatistic['zcountnum'] = 0;
            $newstatistic['solicitation'] =  0;
            $newstatistic['sign'] =  0;
            $newstatistic['way'] =  0;
            $newstatistic['rejection'] = 0;
            
            foreach ($Statistics as $statistic) {
                $newstatistic[$statistic['tracking_status']] = $statistic['tracking_count'];
                $newstatistic['zcountnum'] += $statistic['tracking_count'];
            }
            $result = array("total" => $list->total(), "rows" => $lists, "Statistics" => $newstatistic);
            return json($result);
        }
        return $this->view->fetch();
    }

}
